﻿using MrPP.SocketSystem;
using MrPP.Tracking;
using MrPP.UX;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace MrPP.SerriedCabinet
{
    public class WindowManager : GDGeek.Singleton<WindowManager>
    {


        [SerializeField]
        public ScreenText _screen = null;
        [SerializeField]
        public GameObject _operation = null;
        [SerializeField]
        public ButtonList _buttons;
        [SerializeField]
        public CabinetManager _cabinets;
        [SerializeField]
        public StartWindow _start;

        [SerializeField]
        public MainWindow _main;
          
        public void Start()
        {
            Model.Instance.window.onState += doState;
            Model.Instance.window.onSelected += doSelected;
            doSelected(-1, -1);
            refresh();
            if (DirectionalIndicatorManager.IsInitialized) { 
                DirectionalIndicatorManager.Instance.create(this.transform);
            } 
        }
        public void doState(WindowModel.State oldValue, WindowModel.State newValue) {
            refresh();
        }

        public void doSelected(int last, int next) {
            if (last != -1) {
                var button = this._buttons.getButton(last);
                button.state = UX.Button.State.Enabled;
                button.refresh();
            }
            if (next != -1)
            {
                var button = this._buttons.getButton(next);
                button.state = UX.Button.State.Selected;
                button.refresh();
                _operation.SetActive(true);
            }
            else{
                _operation.SetActive(false);
            }

        }
        public void refresh()
        {
            _start.gameObject.SetActive(false);
            _main.gameObject.SetActive(false);
            if (Model.Instance.window.state == WindowModel.State.None)
            {
            }
            if (Model.Instance.window.state == WindowModel.State.Start)
            {
                _start.gameObject.SetActive(true);
            }
            else if (Model.Instance.window.state == WindowModel.State.Main)
            {
                _main.gameObject.SetActive(true);
            }


        }
        public void OnDestroy() {
            if (Model.IsInitialized) { 
                Model.Instance.window.onState -= doState;
                Model.Instance.window.onSelected -= doSelected;
            }
        }




    }
}